import GoEasy from 'goeasy';

export const goEasyInstance = GoEasy.getInstance({
    host: 'hangzhou.goeasy.io', //新加坡host：singapore.goeasy.io
    appkey: "BC-10abd83a06e74e1e85bc164ff27abc3a", //替换为您的应用appkey
    modules: ['pubsub']
})

const ConnectStatus = {
    SUCCESS: 1,
    FAIL: 0
}

class MyGoEasy {
    // 重试次数
    retryTimes = 5;
    host = "hangzhou.goeasy.io";
    appkey = "BC-10abd83a06e74e1e85bc164ff27abc3a";
    modules = ['pubsub'];
    goEasy;
    // 是否连接成功 0否 1是
    status;

    constructor(host, appkey, modules) {
        this.host = host || "hangzhou.goeasy.io";
        this.appkey = appkey || "BC-10abd83a06e74e1e85bc164ff27abc3a";
        this.modules = modules || ['pubsub'];
        this.goEasy = GoEasy.getInstance({
            host: this.host,
            appkey: this.appkey,
            modules: this.modules
        });
        this.connect();
    }


    // 连接
    connect() {
        //建立连接
        this.goEasy.connect({
            onSuccess: () => { //连接成功
                console.log("连接成功") //连接成功
                this.status = ConnectStatus.SUCCESS;
            },
            onFailed: (error) => { //连接失败
                console.log("连接失败, code:" + error.code + ",error:" + error.content);
                this.status = ConnectStatus.FAIL;
            },
            onProgress: function (attempts) { //连接或自动重连中
                console.log("连接中...", attempts);
            }
        })
    }

    /**
     * 订阅
     * @param channel 频道
     * @returns {Promise<unknown>} 信息
     */
    subscribe(channel) {
        // if (this.status === ConnectStatus.FAIL) {
        //     if (this.retryTimes <= 0) {
        //         this.retryTimes = 5;
        //         return Promise.reject();
        //     }
        //     this.retryTimes--
        //     this.connect();
        //     return this.subscribe(channel);
        // }
        return new Promise((rs, rj) => {
            //订阅消息
            this.goEasy.pubsub.subscribe({
                channel,//替换为您自己的channel
                onMessage: function (message) { //收到消息
                    console.log(`【${message.channel}】频道 接收到消息:${message.content}`);
                    rs(message)
                },
                onSuccess: function () {
                    console.log(`【${channel}】频道订阅成功。`);
                },
                onFailed: function (error) {
                    console.error(`【${channel}】频道订阅失败, 错误编码：${error.code} 错误信息：${error.content}`)
                    rj(error)
                }
            });
        });
    }

    /**
     * 发送消息
     * @param channel 频道
     * @param message 信息
     */
    publish(channel, message) {
        // if (this.status === ConnectStatus.FAIL) {
        //     if (this.retryTimes <= 0) {
        //         this.retryTimes = 5;
        //         return;
        //     }
        //     this.retryTimes--
        //     this.connect();
        //     this.publish(channel, message);
        //     return;
        // }
        //发送
        this.goEasy.pubsub.publish({
            channel,//替换为您自己的channel
            message,//替换为您想要发送的消息内容
            onSuccess: function () {
                console.log("消息发布成功。");
            },
            onFailed: function (error) {
                console.log("消息发送失败，错误编码：" + error.code + " 错误信息：" + error.content);
            }
        });
    }

    /**
     * 取消订阅
     * @param channel 频道
     */
    unsubscribe(channel) {
        this.goEasy.pubsub.unsubscribe({
            channel,
            onSuccess: function () {
                alert("订阅取消成功。");
            },
            onFailed: function (error) {
                alert("取消订阅失败，错误编码：" + error.code + " 错误信息：" + error.content)
            }
        });
    }

    /**
     * 断开连接
     */
    disconnect() {
        //断开连接
        this.goEasy.disconnect({
            onSuccess: function () {
                console.log("断开连接成功")
            },
            onFailed: function (error) {
                console.log("断开连接失败, code:" + error.code + ",error:" + error.content);
            }
        });

    }

}

export const goEasy = new MyGoEasy(null, null, null);
